package com.jack.thinking.java._22.swing.pane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.utils.SwingConsole;

/**
 * 画正玄波
 * @author wangbingwu
 *
 */
public class SineDraw extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int SCALEFACTOR = 200;
	private int cycles;
	private int points;
	private double[] sines;
	private int[] pts;
	
	public SineDraw() {
		setCycles(5);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);              //必须调用， 在 组件必须被重新绘制的时候调用它， 一般放在第一句，何时调用有 Swing 决定。
		int maxWidth = getWidth();
		double hstep = (double)maxWidth / (double)points;
		int maxHeight = getHeight();
		pts = new int[points];
		for(int i = 0 ; i < points; i++) {
			pts[i] = (int)(sines[i] * maxHeight/2 * 0.95 + maxHeight/2);
		}
		g.setColor(Color.RED);
		for(int i = 1; i < points; i++) {
			int x1 = (int)((i-1) * hstep);
			int x2 = (int)(i * hstep);
			int y1 = pts[i-1];
			int y2 = pts[i];
			g.drawLine(x1, y1, x2, y2);
		}
	}
	
	public void setCycles(int newCycles) {
		cycles = newCycles;
		points = SCALEFACTOR * cycles * 2;
		sines = new double[points];
		for(int i = 0 ; i < points ; i++) {
			double radians = (Math.PI / SCALEFACTOR) * i;
			sines[i] = Math.sin(radians);
		}
		repaint();
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new SineWave(), 700, 400);
	}
}

class SineWave extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SineDraw sines = new SineDraw();
	private JSlider adjustCycles = new JSlider(1, 30, 5);
	
	public SineWave() {
		add(sines);
		adjustCycles.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				sines.setCycles(((JSlider)e.getSource()).getValue());
			}
		});
		add(BorderLayout.SOUTH, adjustCycles);
	}
	
}
